package com.example.demo.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    //1.创建Realm对象，自定义类
   public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("dWManger") DefaultWebSecurityManager defaultWebSecurityManager){
       ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
       //设置安全管理器
       shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
       /*
       * 添加Shiro内置过滤器
       * anon 无需认证就可以访问
       * authc 必须认证了才能访问
       * perms 拥有对某个资源的权限才能访问
       * user 必须有 记住我功能 才可以用
       * role 拥有指定角色才能访问
       *
       * */
       Map<String,String> map=new LinkedHashMap<>();
       /*map.put("/test/add","perms[user:add]");
      *//* map.put("/test/*","perms[user:all]");*//*
       map.put("/test/update","perms[user:update]");*/
        map.put("/course/*","authc");
        map.put("/student/*","authc");
        map.put("/admin/*","authc");
        map.put("/teacher/*","authc");
       shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
       //未认证跳转登录界面
       shiroFilterFactoryBean.setLoginUrl("/toLogin");
       //未授权登录界面
       shiroFilterFactoryBean.setUnauthorizedUrl("/weishouquan");
       return shiroFilterFactoryBean;

   }
    //2.创建Realm对象，自定义类
    @Bean(name = "dWManger")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager bean=new DefaultWebSecurityManager();
        //关联Realm
        bean.setRealm(userRealm);
        return bean;
    }


    //3.创建Realm对象，自定义类
    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }

}
